package com.ujs.edu.medicine.controller;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.ujs.edu.medicine.dao.*;
import com.ujs.edu.medicine.dataobject.Author;
import com.ujs.edu.medicine.dataobject.YunjiayiIdcard;
import com.ujs.edu.medicine.dataobject.YunjiayiNotice;
import com.ujs.edu.medicine.dataobject.YunjiayiUser;
import com.ujs.edu.medicine.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:
 * @Date: Created in 20:22 2020/9/28
 * Description: 管理员
 * Modified By:
 */

@RestController
public class AdminController {

    @Autowired
    private YunjiayiUserMapper yunjiayiUserMapper;
    @Autowired
    private YunjiayiIdcardMapper yunjiayiIdcardMapper;
    @Autowired
    private YunjiayiNoticeMapper yunjiayiNoticeMapper;
    @Autowired
    private YunjiayiMessageMapper yunjiayiMessageMapper;
    @Autowired
    private YunjiayiOrderMapper yunjiayiOrderMapper;
    @Autowired
    private YunjiayiLikeMapper yunjiayiLikeMapper;
    @Autowired
    private CollectUSMapper collectUSMapper;
    @Autowired
    private PictureMSMapper pictureMSMapper;
    @Autowired
    private YunjiayiPrescriptionMapper yunjiayiPrescriptionMapper;
    @Autowired
    private YunjiayiPostMapper yunjiayiPostMapper;
    @Autowired
    private LiketableCommentUserMapper liketableCommentUserMapper;
    @Autowired
    private YunjiayiCommentMapper yunjiayiCommentMapper;
    @Autowired
    private YunjiayiShopMapper yunjiayiShopMapper;

    @Autowired
    private HttpServletRequest request;

    public String gettoken(){
        return request.getHeader("Authorization");
    }

    /*获取用户列表信息*/
    @RequestMapping("/api/admin/user/list")
    public Result getUserList(){
        Result result=new Result();
        List<Object> list=new ArrayList<>();
        JSONObject object=null;
        Author author=null;
        JSONObject jsonObject=new JSONObject();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid= JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登陆");
            return result;
        }


        YunjiayiUser user=yunjiayiUserMapper.selectByPrimaryKey(myuserid);

        if (user.getIdentityId()!=6||user.getStatusID()!=6){
            result.setCode(1);
            result.setMessage("您不是管理员或者没有验证通过");
            return result;
        }

        List<YunjiayiUser>users1=yunjiayiUserMapper.selectAll();

        List<YunjiayiUser> users=yunjiayiUserMapper.selectUesrByidentity();
        try {
            for (YunjiayiUser user1:users) {

                object = new JSONObject();

                if (user.getUser_Id() == user1.getUser_Id()) {
                    object.put("isMy", true);
                } else {
                    object.put("isMy", false);
                }

                author = yunjiayiUserMapper.selectPartInfo(user1.getUser_Id());

                object.put("author", author);
                object.put("username", user1.getUsername());
                object.put("phoneNumber", user1.getPhonenumber());
                //加入identify
                JSONObject object1 = new JSONObject();
                object1.put("id", user1.getIdentityId());
                YunjiayiIdcard idcard=yunjiayiIdcardMapper.selectByPrimaryKey(user.getIdentityId());
                object1.put("value", idcard.getValue());
                object.put("identify", object1);

                list.add(object);
            }

            jsonObject.put("list",list);
            result.setCode(0);
            result.setMessage("查询成功");
            result.setData(jsonObject);
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("查询失败");
        }

        return result;
    }


    /*修改用户身份*/
    @RequestMapping("/api/admin/indentify")
    public Result modifyIdentify(@RequestBody JSONObject object){
        Result result=new Result();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid= JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登陆");
            return result;
        }

        YunjiayiUser user=yunjiayiUserMapper.selectByPrimaryKey(myuserid);

        if (user.getIdentityId()!=6||user.getStatusID()!=6){
            result.setCode(1);
            result.setMessage("您不是管理员或者没有验证通过");
            return result;
        }

        YunjiayiUser user1=yunjiayiUserMapper.selectByPrimaryKey(object.getInteger("userID"));

        user1.setIdentityId(object.getInteger("identifyID"));

        int updateIden=yunjiayiUserMapper.updateByPrimaryKey(user1);

        if (updateIden==1){
            result.setCode(0);
            result.setMessage("查询成功");
        }else {
            result.setCode(1);
            result.setMessage("查询失败");
        }

        return result;
    }


    /*修改用户信息*/
    @RequestMapping("/api/admin/realname")
    public Result selectName(@RequestBody JSONObject object){
        Result result=new Result();

        String token=gettoken();
        int myuserid=0;
        try {
            myuserid= JWT.decode(token).getClaim("id").asInt();
        }catch (Exception e){
            result.setCode(1);
            result.setMessage("您还没有登陆，请登陆");
            return result;
        }

        YunjiayiUser user=yunjiayiUserMapper.selectByPrimaryKey(myuserid);

        if (user.getIdentityId()!=6||user.getStatusID()!=6){
            result.setCode(1);
            result.setMessage("您不是管理员或者没有验证通过");
            return result;
        }

        YunjiayiUser user1=yunjiayiUserMapper.selectByPrimaryKey(object.getInteger("userID"));

        user1.setUsername(object.getString("username"));
        user1.setRealname(object.getString("realname"));
        user1.setPhonenumber(object.getString("phoneNumber"));

        int updateUser=yunjiayiUserMapper.updateByPrimaryKey(user1);

        if (updateUser!=0){
            result.setCode(0);
            result.setMessage("修改成功");
        }else {
            result.setCode(1);
            result.setMessage("修改失败");
        }


        return result;
    }



    /*重置密码*/
    @RequestMapping("/api/admin/password")
    public Result  resetPassword(@RequestBody JSONObject object){
        Result result = new Result();

        String token = gettoken();
        int userId = 0;
        try {
            userId = JWT.decode(token).getClaim("id").asInt();
        } catch (Exception e) {
            result.setCode(1);
            result.setMessage("您还没有登陆，请登陆");
            return result;
        }

        YunjiayiUser user=yunjiayiUserMapper.selectByPrimaryKey(object.getInteger("userID"));
        user.setPassword(object.getString("password"));

        int updatePassword=yunjiayiUserMapper.updateByPrimaryKey(user);

        if (updatePassword!=0){
            result.setCode(0);
            result.setMessage("重置成功");
        }else {
            result.setCode(1);
            result.setMessage("重置失败");
        }

        return result;
    }

}
